﻿#include "stdafx.h"
#include "../../GameCore/CGame.h"


void CAnimal::Init(int id, int type, float baseX, float baseY)
{
	_id = id;
	_type = type;
	_baseX = baseX;
	_baseY = baseY;
	_targetY = 0.0;
	_last_time_spawn = GET_GAME()->GetTime();
	_state = ANIMAL_STATE_MOVE;
	_speed = ANIMAL_SPEED;
	if(_baseX <= k_SCREEN_WIDTH/2)
	{
		_targetX = k_SCREEN_WIDTH - 37;
	}
	else 
	{
		_targetX = 0.0;
	}
}

void CAnimal::Update()
{
	//printf("animal type %d update", _type);
	if(MoveTo(_targetX, _targetY))
	{
		if(_targetX == 0.0)
			_targetX = k_SCREEN_WIDTH - 37;
		else
			_targetX = 0.0;
	}
	float time = GET_GAME()->GetTime();
	if(time - _last_time_spawn >= ANIMAL_DEFAULT_TIME_SPAWN*1000)
	{
		GET_GAME()->CreateItem(_baseX, _baseY);
		_last_time_spawn = time;
	}
}

void CAnimal::Render()
{
	GET_SPRITE().DrawTexture(GL(), _type, _baseX, _baseY);
}

bool CAnimal::MoveTo(float targetX, float targetY)
{	
	if(targetX > 0.0)
	{
		_baseX += _speed;
		if(_baseX >= targetX)
			return true;
	}
	else
	{
		_baseX -= _speed;
		if(_baseX <= targetX)
			return true;
	}
	return false;
}

